package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import one.stand.model.ResultModel;
import one.stand.model.essay.EssayTypeModel;
import one.stand.request.essay.EssayTypeForm;
import one.stand.request.essay.EssayTypeQuery;
import one.stand.request.essay.EssayTypeRequest;
import one.stand.response.essay.EssayTypeResponse;
import one.stand.service.BaseService;
import one.stand.service.EssayTypeService;
import one.stand.util.DateUtil;
import one.stand.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 获客文章分类(CustomerEssayType)表服务实现类
 *
 * @author makejava
 * @since 2020-05-06 16:38:43
 */
@Service
@Transactional
public class EssayTypeServiceImpl  extends BaseService implements EssayTypeService {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ResultModel<EssayTypeResponse> queryById(Long id) {
        log.info("queryById:",id);
        EssayTypeResponse response = new EssayTypeResponse();
        EssayTypeModel essayTypeModel =  this.essayTypeMapper.queryById(id);
        BeanUtils.copyProperties(essayTypeModel, response);
        return ResultModel.success(response);
    }

    @Override
    public ResultModel<Boolean> update(final EssayTypeForm request) {
        log.info("essaytype_update:",request);
        EssayTypeModel oldEssay = this.essayTypeMapper.queryById(request.getEssayTypeId());
        if(oldEssay == null){
//            throw new Servicee
            return ResultModel.noData();
        }
        BeanUtils.copyProperties(request,oldEssay, getNullPropertyNames(request));
        oldEssay.setId(request.getEssayTypeId());
        final int result = this.essayTypeMapper.update(oldEssay);
        if(result > 0){
            return ResultModel.success();
        }
        return ResultModel.fail();
    }

    @Override
    public ResultModel<Boolean> insert(final EssayTypeRequest request) {
        log.info("essaytype_insert:",request);

        EssayTypeModel essayTypeModel = new EssayTypeModel();
        BeanUtils.copyProperties(request,essayTypeModel);
        essayTypeModel.setCompanyId(request.getCId());
        essayTypeModel.setCreatedTime(DateUtil.now());
        if(request.getSort() == null){
            essayTypeModel.setSort(1);
        }
        essayTypeModel.setDeleted(0);
        //按名称公司查询是否存在
        if(this.essayTypeMapper.queryNameCompany(essayTypeModel) == null){
            if(essayTypeMapper.insert(essayTypeModel) > 0){
                return ResultModel.success();
            }else{
                return ResultModel.fail();
            }
        }
        return ResultModel.fail("分类已经存在");
    }

    @Override
    public ResultModel<PageInfo<EssayTypeResponse>> queryPage(EssayTypeQuery request) {
        log.info("queryPage:",request);
        EssayTypeModel model = new EssayTypeModel();
        BeanUtils.copyProperties(request, model);
        model.setCompanyId(request.getCId());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        log.info("查询文章分类列表参数: {}", JSON.toJSONString(model));
        List<EssayTypeModel> list = essayTypeMapper.queryAll(model);
        List<EssayTypeResponse> essayTypeResponses = new ArrayList<>(0);
        list.forEach(essayTypeModel -> {
            {
                EssayTypeResponse response = new EssayTypeResponse();
                BeanUtils.copyProperties(essayTypeModel, response);
                Integer num = this.dynamicMapper.queryDynamic(response.getId());
                response.setArticlesNum(Optional.ofNullable(num).isPresent() ? num : 0);
                essayTypeResponses.add(response);
            }
        });
        PageInfo<EssayTypeResponse> pageInfo = new PageInfo(essayTypeResponses);
        return ResultModel.success(pageInfo);
    }

    @Override
    public ResultModel<Boolean> deleteById(Long essayTypeId) {
        if(essayTypeMapper.deleteById(essayTypeId.intValue()) > 0 ){
            return ResultModel.success();
        }
        return ResultModel.fail();
    }

    @Override
    public ResultModel<List<EssayTypeResponse>> queryAllType(EssayTypeQuery request) {
        EssayTypeModel model = new EssayTypeModel();
        BeanUtils.copyProperties(request, model);
        model.setCompanyId(request.getCId());
        List<EssayTypeModel> list = essayTypeMapper.queryAll(model);
        List<EssayTypeResponse> essayTypeResponses = new ArrayList<>(0);
        list.forEach(essayTypeModel -> {
            {
                EssayTypeResponse response = new EssayTypeResponse();
                BeanUtils.copyProperties(essayTypeModel, response);
                essayTypeResponses.add(response);
            }
        });
        return ResultModel.success(essayTypeResponses);
    }


}
